package com.teacherprofile.manage.controller;

import java.util.List;
import com.teacherprofile.common.utils.StringUtils;
import com.teacherprofile.manage.domain.dto.RefresherTrainingsExportDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.teacherprofile.common.annotation.Log;
import com.teacherprofile.common.core.controller.BaseController;
import com.teacherprofile.common.core.domain.AjaxResult;
import com.teacherprofile.common.enums.BusinessType;
import com.teacherprofile.common.domain.RefresherTrainings;
import com.teacherprofile.manage.service.IRefresherTrainingsService;
import com.teacherprofile.common.utils.poi.ExcelUtil;
import com.teacherprofile.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;

/**
 * 进修培训Controller
 *
 * @author qvtu
 * @date 2025-05-16
 */
@Api(tags = "进修培训")
@RestController
@RequestMapping("/manage/trainings")
public class RefresherTrainingsController extends BaseController {
    @Autowired
    private IRefresherTrainingsService refresherTrainingsService;

    /**
     * 查询进修培训列表
     */
    @ApiOperation(value = "查询进修培训列表")
    @PreAuthorize("@ss.hasPermi('manage:trainings:list')")
    @GetMapping("/list")
    public TableDataInfo list(RefresherTrainings refresherTrainings) {
        startPage();
        List<RefresherTrainings> list = refresherTrainingsService.selectRefresherTrainingsList(refresherTrainings);
        return getDataTable(list);
    }

    /**
     * 导出进修培训列表
     */
    @ApiOperation(value = "导出进修培训列表")
    @PreAuthorize("@ss.hasPermi('manage:trainings:export')")
    @Log(title = "进修培训", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@Valid RefresherTrainingsExportDTO dto) {
        List<RefresherTrainings> list = refresherTrainingsService.selectRefresherTrainingsList(dto);
        ExcelUtil<RefresherTrainings> util = new ExcelUtil<RefresherTrainings>(RefresherTrainings.class);
        util.exportEasyExcel(response, list, "进修培训数据", StringUtils.str2List(dto.getExportFields(), ",", true, true));
    }

    /**
     * 获取进修培训详细信息
     */
    @ApiOperation(value = "获取进修培训详细信息")
    @PreAuthorize("@ss.hasPermi('manage:trainings:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(refresherTrainingsService.selectRefresherTrainingsById(id));
    }

    /**
     * 新增进修培训
     */
    @ApiOperation(value = "新增进修培训")
    @PreAuthorize("@ss.hasPermi('manage:trainings:add')")
    @Log(title = "进修培训", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RefresherTrainings refresherTrainings) {
        return toAjax(refresherTrainingsService.insertRefresherTrainings(refresherTrainings));
    }

    /**
     * 修改进修培训
     */
    @ApiOperation(value = "修改进修培训")
    @PreAuthorize("@ss.hasPermi('manage:trainings:edit')")
    @Log(title = "进修培训", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RefresherTrainings refresherTrainings) {
        return toAjax(refresherTrainingsService.updateRefresherTrainings(refresherTrainings));
    }

    /**
     * 删除进修培训
     */
    @ApiOperation("删除进修培训")
    @PreAuthorize("@ss.hasPermi('manage:trainings:remove')")
    @Log(title = "进修培训", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(refresherTrainingsService.deleteByIds(ids));
    }

    /**
     * 导入进修培训
     *
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation("导入进修培训")
    @Log(title = "导入管理", businessType = BusinessType.IMPORT)
    // TODO 权限补充
    @PreAuthorize("@ss.hasPermi('system:trainings:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<RefresherTrainings> util = new ExcelUtil<RefresherTrainings>(RefresherTrainings.class);
        List<RefresherTrainings> refresherTrainings = util.importEasyExcel(file.getInputStream());
        return success(refresherTrainingsService.importData(refresherTrainings));
    }

    /**
     * 审批
     */
    @ApiOperation(value = "进修培训审批")
    @PreAuthorize("@ss.hasPermi('manage:trainings:check')")
    @Log(title = "进修培训", businessType = BusinessType.UPDATE)
    @PutMapping("/check")
    public AjaxResult check(@RequestBody RefresherTrainings refresherTrainings) {
        return toAjax(refresherTrainingsService.check(refresherTrainings));
    }
}
